﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;


namespace FXnaInfrastructure
{

    /// <summary>
    /// 提供id与描述的基类
    /// </summary>
    [Serializable]
    public class FEntity:PropertyChangedBase,ICloneable
    {


        /// <summary>
        /// 为基类提供默认GUID的选项
        /// </summary>
        public FEntity():this(Guid.NewGuid().ToString())
        {

 
        }


        public FEntity(string id)
        {
            this._id = id;
            this._desc = id;

        }

        [field:NonSerialized]
        public event EventHandler DescChanged;



        string _id;

        [Browsable(false)]
        public string Id
        {
            get { return _id; }
            
        }


        string _desc;

        [DisplayName("名称")]
        public virtual string Desc
        {
            get { return _desc; }
            set {

                if (this._desc!=value)
                {

                    _desc = value;

                    //两个事件目前是都触发，之后协调会去掉一个

                    OnPropertyChanged("Desc");

                    if (DescChanged!=null)
                    {
                        DescChanged(this,EventArgs.Empty);
                        
                    }

                    
                }

                 }
        }



        //重写相等方法，便于日后的代码书写与测试，提供entity的相等判断

        #region Equality Tests

        /// <summary>
        /// Determines whether the specified entity is equal to the 
        /// current instance.
        /// </summary>
        /// <param name="entity">An <see cref="System.Object"/> that 
        /// will be compared to the current instance.</param>
        /// <returns>True if the passed in entity is equal to the 
        /// current instance.</returns>
        public override bool Equals(object entity)
        {
            if (entity == null || !(entity is FEntity))
            {
                return false;
            }
            return (this == (FEntity)entity);
        }

        /// <summary>
        /// Operator overload for determining equality.
        /// </summary>
        /// <param name="base1">The first instance of an 
        /// <see cref="EntityBase"/>.</param>
        /// <param name="base2">The second instance of an 
        /// <see cref="EntityBase"/>.</param>
        /// <returns>True if equal.</returns>
        public static bool operator ==(FEntity base1,
            FEntity base2)
        {
            // check for both null (cast to object or recursive loop)
            if ((object)base1 == null && (object)base2 == null)
            {
                return true;
            }

            // check for either of them == to null
            if ((object)base1 == null || (object)base2 == null)
            {
                return false;
            }

            if (base1.Id != base2.Id)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Operator overload for determining inequality.
        /// </summary>
        /// <param name="base1">The first instance of an 
        /// <see cref="EntityBase"/>.</param>
        /// <param name="base2">The second instance of an 
        /// <see cref="EntityBase"/>.</param>
        /// <returns>True if not equal.</returns>
        public static bool operator !=(FEntity base1,
            FEntity base2)
        {
            return (!(base1 == base2));
        }

        /// <summary>
        /// Serves as a hash function for this type.
        /// </summary>
        /// <returns>A hash code for the current Key 
        /// property.</returns>
        public override int GetHashCode()
        {
            return this._id.GetHashCode();
        }

        #endregion



        public virtual object Clone()
        {
            return this.MemberwiseClone();
        }
    }
}
